﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Adp;
using Adp.Common;
using Microsoft.AspNetCore.Mvc;
using Yqd.Core;
using Yqd.Dto;
using Yqd.Models;

namespace Yqd.Controllers
{
    public class UnitLeaderController : BaseController
    {
        YqdContext ctx;
        DdService ddService;

        public UnitLeaderController(YqdContext ctx, DdService ddService)
        {
            this.ctx = ctx;
            this.ddService = ddService;
        }
        public ActionResult GetList()
        {
            var start = Form.GetInt("start");
            var limit = Form.GetInt("limit");
            var year = Form.GetString("year","2023");
            var key = Form.GetString("key");
            var unit = Form.GetString("unit");
            string where = "";        
            if (!string.IsNullOrEmpty(key))
            {
                where += $" and leader like '%{key}%' or TaskPath like '%{key}%'";
            }
            if (!string.IsNullOrEmpty(year))
            {
                where += $" and year like '%{year}%'";
            }
            List<string> unitCode = new List<string>();
            if (!string.IsNullOrEmpty(unit))
            {
                var listUnit = ctx.Yqd_Unit.Where(x => x.Name.Contains(unit)).ToList();
               if(listUnit.Count > 0)
                {
                    unitCode = listUnit.Select(x => x.UnitCode).ToList();
                }
            }
            if (unitCode.Any())
            {
                var sql = string.Join(" or ", unitCode.Select(x => $" unit like '%{x}%' "));
                where += " and " + sql;
            }
            where += " and isshow=1";
            var (totalcount, data) = ctx.Database.SqlPager<Yqd_UnitLeader>(start, limit, "*", "1=1 "+ where, "[Tactic] asc", "");
            var list = data.Select(x => new Yqd_UnitLeaderDto
            {
                ID = x.ID,
                Leader = x.Leader,
                UnitValue = x.Unit,
                Tactic = x.Tactic,
                TaskPath = x.TaskPath,
                Year = x.Year
            }).ToList();
            foreach (var item in list)
            {
                if (!string.IsNullOrEmpty(item.UnitValue))
                {
                    var arr = item.UnitValue.Split(',');
                    var units = ctx.Yqd_Unit.Where(x => arr.Contains(x.UnitCode)).ToList();
                    item.UnitName = string.Join(",", units.Select(x => x.Name));
                }
                else
                {
                    item.UnitValue = "";
                }
            }
            return this.ToJson(new
            {
                success = true,
                totalcount,
                data = list
            });
        }

        public ActionResult Update()
        {
            var year = Form.GetInt("year");
            var leader = Form.GetString("leader");
            var id = Form.GetInt("id");
            var taskPath = Form.GetString("taskPath");
            var leadunit = Form.GetString("leadunit");
            var tactic = Form.GetInt("tactic");
            var unitleader = ctx.Yqd_UnitLeader.Where(x => x.ID == id).Single();
            unitleader.Year = year;
            unitleader.Unit = leadunit;
            unitleader.Leader = leader;
            unitleader.TaskPath = taskPath;
            unitleader.Tactic = tactic;
            var ret = ctx.SaveChanges();
            return this.ToJson(new
            {
                success = true,
                ret
            });
        }
        public ActionResult Add()
        {
            var year = Form.GetInt("year");
            var leader = Form.GetString("leader");
            var taskPath = Form.GetString("taskPath");
            var leadunit = Form.GetString("leadunit");
            var unitleader = new Yqd_UnitLeader();
            unitleader.Year = year;
            unitleader.Unit = leadunit;
            unitleader.Leader = leader;
            unitleader.TaskPath = taskPath;
            unitleader.IsShow = true;
            ctx.Yqd_UnitLeader.Add(unitleader);
            var ret = ctx.SaveChanges();
            return this.ToJson(new
            {
                success = true,
                ret
            });
        }

        public ActionResult Delete()
        {
            var ID = Form.GetInt("ID");
            //var ret = ctx.Database.ExecuteSql("delete from Yqd_UnitLeader where id={0};", ID);
            var ret = ctx.Database.ExecuteSql("UPDATE [Yqd_UnitLeader] SET [IsShow] = 0 WHERE id={0};", ID);            
            return this.ToJson(new
            {
                success = true,
                ret
            });
        }
    }
}
